package com.java46.carera.application.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.Application;
import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.exception.BusinessException;
import com.java46.carera.base.mapper.ApplicationMapper;
import com.java46.carera.base.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 招聘信息逻辑类
 */
@Transactional
@Service
public class ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;

    /**
     * 添加招聘信息
     * @param application
     */
    public JsonResult addApplication(Application application){
        if(application == null){
            throw new BusinessException("添加的信息不能为空！");
        }
        applicationMapper.insert(application);
        return JsonUtil.setJsonResult(true, "添加成功！", null);
    }

    /**
     * 修改招聘信息
     * @param application
     */
    public JsonResult modApplication(Application application){
        if(application.getAppid() == null){
            throw new BusinessException("缺少修改的必要参数！");
        }
        applicationMapper.updateByPrimaryKey(application);
        return JsonUtil.setJsonResult(true, "修改成功！", null);
    }

    /**
     * 删除招聘信息
     * @param appid
     */
    public JsonResult deleteApplication(Integer appid){
        if(appid == null){
            throw new BusinessException("缺少删除的必要参数！");
        }
        applicationMapper.deleteByPrimaryKey(appid);
        return JsonUtil.setJsonResult(true, "删除成功！", null);
    }

    /**
     * 查询所有招聘信息
     * @return
     */
    public JsonResult queryAll(String keyword, Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Application> applications = applicationMapper.selectAll(keyword);
        PageInfo<Application> pages = new PageInfo<>(applications);
        return JsonUtil.setJsonResult(true, "查询成功！", pages);
    }

    /**
     * 根据招聘编号查询招聘信息
     * @param appid
     * @return
     */
    public JsonResult queryApplicationById(Integer appid){
        Application application = applicationMapper.selectByPrimaryKey(appid);
        return JsonUtil.setJsonResult(true, "查询成功！", application);
    }
}
